package com.myTimeUtils.android.TimeTracker.Schedule;
/**
 * This class provides an implementation of simple time. This includes keeping track of days, hours, and minutes. 
 * This will be done using a twenty-four clock.
 *
 * @author Joseph Walker
 */
public class Time implements Comparable{
	private int day;
	/**
	 * The number identifying Friday.
	 */
	public static final int FRIDAY = 4;
	private int hour;
	private int minute;
	/**
	 * The number identifying Monday.
	 */
	public static final int MONDAY = 0;
	/**
	 * The number identifying Saturday.
	 */
	public static final int SATURDAY = 5;
	private int second;
	/**
	 * The number identifying Sunday.
	 */
	public static final int SUNDAY = 6;
	/**
	 * The number identifying Thursday.
	 */
	public static final int THURSDAY = 3;
	/**
	 * The number identifying Tuesday
	 */
	public static final int TUESDAY = 1; 
	/**
	 * The number identifying Wednesday
	 */
	public static final int WEDNESDAY = 2;
	
	/**
	 * Constructs a time object from the provided values. The day should be set
	 * using the static member implementation of the day.
	 *
	 * @param day				The day of this time
	 * @param hour				The hour of this time
	 * @param minute			The minute of this time
	 */
	public Time(int day, int hour, int minute){
		this(day, hour, minute, 0);
	}
	/**
	 * Constructs a time object from the provided values. The day should be set
	 * using the static member implementation of the day.
	 *
	 * @param day				The day of this time
	 * @param hour				The hour of this time
	 * @param minute			The minute of this time
	 * @param second			The second of this time
	 */
	public Time(int day, int hour, int minute, int second){
		this.setDay(day);
		this.setHour(hour);
		this.setMinute(minute);
		this.setSecond(second);
	}
	/**
	 * Compares this time to the given object. This function returns 1 if this time is larger, 
	 * -1 if the given time is larger, and 0 if they are equal.
	 *
	 * @param o		The object to compare to
	 * @return		An int value quantifying the equality	
	 */
	public int compareTo(Object o){
		Time time = (Time)o;
		if(this.day > time.day){ return 1 ;}
		else if(this.day < time.day){ return -1; }
		else{
			if(this.getHour() > time.getHour()){ return 1; }
			else if(this.getHour() < time.getHour()){ return -1; }
			else{
				if(this.getMinute() > time.getMinute()){ return 1; }
				else if(this.getMinute() < time.getMinute()){ return -1; }
			}		
		}
		return 0;
	}
	/**
	 * Compares the given object to this time. Returns true if they are equal.
	 *
	 * @param o					the object to compare to this time
	 * @return 					true if the objects are equal
	 */
	public boolean equals(Object o){
		if(o instanceof Time) return equals((Time)o);
		return false;
	}
	private boolean equals(Time time){
		return day == time.day && this.getHour() == time.getHour() && this.getMinute() == getMinute();
	}
	/**
	 * Returns the day, in string format, of this time.
	 *
	 * @return				The day, in string format
	 */
	public String getDay(){
		switch(this.day){
			case MONDAY:	return "Monday";
			case TUESDAY:	return "Tuesday";
			case WEDNESDAY:	return "Wednesday";
			case THURSDAY:	return "Thursday";
			case FRIDAY:	return "Friday";
			case SATURDAY:	return "Saturday";
			case SUNDAY:	return "Sunday";
			default:	return "Error";
		}	
	}
	/**
	 * Returns the hour associated with this time. This is in 
	 * twenty-four hour format.
	 *
	 * @return				The hour
	 */
	public int getHour(){ return this.hour; }
	/**
	 * Returns the minute associated with this time.
	 *
	 * @return				The minute
	 */
	public int getMinute(){ return this.minute; }
	/**
	 * Returns the number of seconds associated with this time.
	 *
	 * @return				The second
	 */
	public int getSecond(){ return this.second; }
	/**
	 * Calculates the difference between the given time and this time. The 
	 * given time must be smaller than this time. The return is an array of 
	 * ints with array[0] being days, array[1] being hours, array[2] being
	 * minutes, and array[3] being the seconds.
	 *
	 * @param time				The time to measure the distance from
	 * @return				The time between the two.
	 */
	public int[] getTimeDifference(Time time){
		//TODO implement exception handeling.
		int seconds = 0;
		int minutes = 0;
		int hours = 0;
		int days = 0;
		seconds += this.getSecond() - time.getSecond();
		if(seconds < 0){
			seconds += 60;
			minutes--;
		}
		minutes += this.getMinute() - time.getMinute();
		if(minutes < 0){
			minutes += 60;
			hours--;
		}
		hours += this.getHour() - time.getHour();
		if(hours < 0){
			hours += 24;
			days--;
		}
		days += this.day - time.day;
		if(days < 0) days+= 7;
		return new int[]{ days, hours, minutes, seconds };
	}
	/**
	 * Sets the day associated with this time. The day should
	 * be set using the static member implementation of the day.
	 *
	 * @param day				The day to associate with this time
	 */
	public void setDay(int day){ this.day = day; }
	/**
	 * Sets the hour associated with this time. This should be done in twenty-four
	 * hour format.
	 *
	 * @param hour				The hour to associate with this time
	 */
	public void setHour(int hour){ this.hour = hour; }
	/**
	 * Sets the minute associated with this time. 
	 *
	 * @param minute			The minute to associate with this time
	 */
	public void setMinute(int minute){ this.minute = minute; }
	/**
	 * Sets the number of seconds associated with this period.
	 *
	 * @param second			The number of seconds associated with this period
	 */
	public void setSecond(int second){ this.second = second; }
	/**
 	 * Returns a string representation of this time.
	 *
	 * @return				A string representation of this time
	 */
	public String toString(){
		String out = this.getDay() + " ";
		if(this.getHour() > 9) out += this.getHour() + ":";
		else out += "0" + this.getHour() + ":";
		if(this.getMinute() > 9) out += this.getMinute() + ":";
		else out += "0" + this.getMinute() + ":";
		if(this.getSecond() > 9) out += this.getSecond() + "";
		else out += "0" + this.getSecond();
		return out;
	}
} 
